package com.ddone.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddone.enums.Log;
import com.ddone.enums.OperateType;
import com.ddone.enums.OperatorType;
import com.ddone.model.auth.SysRole;
import com.ddone.model.auth.SysUser;
import com.ddone.model.vo.Result;
import com.ddone.model.vo.SysUserQueryVo;
import com.ddone.service.SysUserService;
import com.ddone.util.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ddone
 * @date 2024/3/29-10:24
 */
@RestController
@Api(value = "用户管理", tags = "用户管理")
@RequestMapping("/sys-user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("分页查询用户列表")
    @GetMapping("/{page}/{size}")
    public Result<Object> page(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                               @ApiParam(name = "size", value = "每页显示的记录数", required = true) @PathVariable Long size,
                               @ApiParam(name = "vo", value = "查询对象的条件", required = false) SysUserQueryVo vo) {
        Page<SysUser> pageParam = new Page<>(page, size);
        Page<SysUser> pageResult = sysUserService.queryPage(pageParam, vo);
        return Result.ok(pageResult);
    }

    @Log(title = "添加用户",operatorType = OperatorType.MANAGE,operateType = OperateType.INSERT,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @PostMapping
    @ApiOperation("保存用户")
    public Result<Object> save(@ApiParam(name = "sysUser")
                               @Validated
                               @RequestBody SysUser sysUser) {
        sysUser.setPassword(MD5.encrypt(sysUser.getPassword()));
        sysUserService.save(sysUser);
        return Result.ok();
    }

    @Log(title = "删除用户",operatorType = OperatorType.MANAGE,operateType = OperateType.DELETE,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @DeleteMapping("/{id}")
    @ApiOperation("按照ID删除用户")
    public Result<Object> delete(
            @ApiParam("删除用户的ID")
            @PathVariable Long id){
        sysUserService.removeById(id);
        return Result.ok();
    }

    @Log(title = "批量删除",operatorType = OperatorType.MANAGE,operateType = OperateType.INSERT,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @DeleteMapping("/batch")
    @ApiOperation("批量删除数据")
    public Result<SysRole> removeBatch(@ApiParam(name = "idList", value = "删除的ID列表", required = true)
                                       @RequestBody List<Long> idList) {
        sysUserService.removeByIds(idList);
        return Result.ok();
    }

    @Log(title = "修改用户",operatorType = OperatorType.MANAGE,operateType = OperateType.UPDATE,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @PutMapping
    @ApiOperation("修改用户")
    public Result<SysRole> update(@RequestBody @Validated @ApiParam("修改的用户信息") SysUser user) {
        sysUserService.updateById(user);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @GetMapping("/{id}")
    @ApiOperation("按照ID获取用户")
    public Result<Object> get(@ApiParam(name = "id",value = "用户地")
                              @PathVariable Long id) {
        return Result.ok(sysUserService.getById(id));
    }


    @Log(title = "更新用户状态",operatorType = OperatorType.MANAGE,operateType = OperateType.INSERT,isSaveRequestData = true)
    @PutMapping("/{id}/{status}")
    @ApiOperation("更新用户状态")
    public Result<Object> updateStatus(@PathVariable("id") Long id,
                                       @PathVariable("status") Integer status){
        sysUserService.updateStatus(id,status);
        return Result.ok();
    }
}
